﻿using Mapster;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统.自定义类;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.报表窗体
{
    public partial class 垃圾桶 : Form
    {
        private List<RukuTable> rukuTables = new List<RukuTable>();
        public 垃圾桶()
        {
            InitializeComponent();
            var conMenu = new ContexMenuEX() { formName = this.Name, dataGridView  = uiDataGridView1 , menuStrip = contextMenuStrip1,obj=new RukuTable () };
            conMenu.Init();
            CreateGrid.CreateDatagridview(this.Name, uiDataGridView1);
            uiDataGridView1.Columns.Add (CSelect);
            刷新数据();
        }

        private void 清空垃圾桶ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RukuTableService.DeleteRukuTable("delete from RukuTable");
            刷新数据();
        }

        private void 删除垃圾ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uiDataGridView1.EndEdit();
            for (int row = 0; row < uiDataGridView1.RowCount; row++)
            {
                if (uiDataGridView1.Rows[row].Cells[CSelect.Name].Value != null)
                {
                    if ((Boolean)uiDataGridView1.Rows[row].Cells[CSelect.Name].Value)
                    {
                        RukuTableService.DeleteRukuTable(x => x.ID == (int)uiDataGridView1.Rows[row].Cells["ID"].Value);
                    }
                }
            }
            刷新数据();
        }
        private void 刷新数据()
        {
            uiPagination1_PageChanged(null, null, 1, uiPagination1.PageSize);
        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            刷新数据();
        }

        private void txtganghao_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter )
            {
                刷新数据();
            }
        }

        private void 退回库存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uiDataGridView1.EndEdit();
            var stocks = uiDataGridView1.DataSource as List<RukuTable>;
            try
            {
                Connect.dbcontext.BeginTran();
                for (int row = 0; row < uiDataGridView1.RowCount; row++)
                {
                    if (uiDataGridView1.Rows[row].Cells[CSelect.Name].Value != null)
                    {
                        if ((Boolean)uiDataGridView1.Rows[row].Cells[CSelect.Name].Value)
                        {
                            var stock = stocks.FirstOrDefault(x => x.ID == (int)uiDataGridView1.Rows[row].Cells["ID"].Value);
                            Connect.dbcontext.Insertable(stock.Adapt(new StockTable())).ExecuteCommand();
                            Connect.dbcontext .Deleteable(stock).ExecuteCommand();
                            //var juans = JuanHaoTableService.GetJuansWithStock(stock.Adapt(new StockTable())).Where(x => x.Danhao == string.Empty && x.state == 1).ToList();
                            var juans = Connect.dbcontext.Queryable<JuanHaoTable>().Where(x => x.ReceiptNo == stock.RKDH && x.GangHao == stock.GH && x.SampleName == stock.PM && x.yanse == stock.YS && x.Danhao == string.Empty && x.state == 1).ToList();
                            juans.ForEach(x => x.state = 0);
                            Connect.dbcontext.Updateable(juans).ExecuteCommand();
                        }
                    }
                }
                Connect.dbcontext.CommitTran();
                刷新数据();
            }
            catch(Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                ShowMessageBox.ShowErrorBox("退回库存的时候发送了错误:" + ex.Message);
            }
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            int totalnum = 0;
            int totalpage = 0;
            rukuTables = Connect.dbcontext.Queryable<RukuTable>().Where(x => x.GH.Contains(txtganghao.Text) && x.CustomName.Contains(txtCustomer.Text)).ToPageList(pageIndex, count, ref totalnum, ref totalpage);          
            uiDataGridView1.DataSource = rukuTables;
            uiPagination1.TotalCount = totalnum;
            uiDataGridViewFooter1.Clear();
            uiDataGridViewFooter1["BH"] = "合计";
            uiDataGridViewFooter1["MS"] = rukuTables.Sum(x => x.MS).ToString();
            uiDataGridViewFooter1["JS"] = rukuTables.Sum(x => x.JS).ToString();
            uiDataGridViewFooter1["RukuNum"] = rukuTables.Sum(x => x.RukuNum).ToString();
            uiDataGridViewFooter1["FahuoNum"] = rukuTables.Sum(x => x.FahuoNum).ToString();
        }
    }
}
